package com.lenovo.smarttraffic.util;

import android.util.Log;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.lenovo.smarttraffic.InitApp;
import com.lenovo.smarttraffic.api.VolleyCallBack;
import com.lenovo.smarttraffic.ui.activity.LoginActivity;
import com.lenovo.smarttraffic.ui.activity.NetWorkActivity;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by Caleb.F
 * on 2020-03-12
 * at 13:01
 */
public class VolleyUtil {
    private static final String PORT="8088";
    private static RequestQueue mQueue;
    public static void post(String url, JSONObject jsonObject, VolleyCallBack volleyCallBack){
        String httpurl = new StringBuilder().append("http://").append(NetWorkActivity.getIP(InitApp.getInstance()))
                .append(":").append(PORT).append("/transportservice/action/")
                .append(url).toString();
        if(!jsonObject.has("UserName")){
            try {
                jsonObject.put("UserName", LoginActivity.getUserName(InitApp.getInstance()));
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(httpurl, jsonObject, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                volleyCallBack.onSucceess(jsonObject, jsonObject.toString());
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                volleyCallBack.onFailure(volleyError.toString());
            }
        });
        if(mQueue==null){
            mQueue= Volley.newRequestQueue(InitApp.getInstance());
        }
        mQueue.add(jsonObjectRequest);
    }
}
